package Demo;

import com.sun.org.apache.xpath.internal.operations.Mod;

import java.lang.reflect.*;

public class PersonTest {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        //示列2
        Class clz = Person.class;
        String fullName = clz.getName();
        String simpleName = clz.getSimpleName();
        System.out.println("以下是"+fullName+"类的基本信息");
        System.out.println("----------------------------");

        Package pkg = clz.getPackage();
        System.out.println(simpleName+"定义在:"+pkg.getName()+"包中");
        System.out.println("----------------------------");

        Class superClass = clz.getDeclaringClass();
        System.out.println(simpleName+"类的超类是:"+superClass.getName());
        System.out.println("----------------------------");

        Class[] interfaces = clz.getInterfaces();
        System.out.println(simpleName+"类的实现的接口:");
        for (Class c : interfaces)
            System.out.println("\t"+c.getName());
        System.out.println("----------------------------");

        int modifier = clz.getModifiers();
        System.out.println(simpleName+"类的修饰符:");
        if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
            System.out.println("\t访问修饰符是:public");
        else
            System.out.println("\t访问修饰符是:default(package)");
        if ((modifier & Modifier.FINAL) == Modifier.FINAL)
            System.out.println("\t这个类是final的");
        if ((modifier & Modifier.ABSTRACT) == Modifier.ABSTRACT)
            System.out.println("\t这是一个抽象类");
        if ((modifier & Modifier.INTERFACE) == Modifier.INTERFACE)
            System.out.println("\t这是一个接口");
        System.out.println("----------------------------");

        //示列3
        Constructor[] cons = Person.class.getDeclaredConstructors();

        System.out.println("==============构造方法展示==============");
        for (Constructor con : cons){
            System.out.println("访问修饰符");
            int modifiers = con.getModifiers();
            if ((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                System.out.println("public");
            else if ((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                System.out.println("protected");
            else if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                System.out.println("private");
            else
                System.out.println("default(package)");

            Class[] params = con.getParameterTypes();
            if (params.length == 0){
                System.out.println("该构造方法没有参数");
            }else {
                System.out.println("该构造方法的参数列表为:[");
                for (int i = 0; i<params.length;i++){
                    if (i != 0)
                        System.out.println(",");
                    System.out.println(params[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("-------------------------------------");
        }
        //示列4
        Field[] fields = Person.class.getDeclaredFields();
        System.out.println("==============属性展示=============");
        for (Field field : fields){
            System.out.println("属性名:"+field.getName());
            System.out.println("类型:"+field.getType().getName());

            System.out.println("访问修饰符:");
            int modifiers = field.getModifiers();
            if ((modifier & Modifier.STATIC) == Modifier.STATIC)
                System.out.println("这是一个静态属性");
            if ((modifier & Modifier.FINAL) == Modifier.FINAL)
                System.out.println("这是一个final属性");
            System.out.println("-------------------------------------");
        }
        //示列5
        Method[] methods = Person.class.getDeclaredMethods();
        System.out.println("=============方法展示============");
        for (Method method : methods){
            System.out.println("方法名:"+method.getName());
            System.out.println("返回值类型:" + method.getReturnType().getName());
            Class[] params = method.getParameterTypes();
            System.out.println("访问修饰符:");
            int modifiers = method.getModifiers();
            if ((modifier & Modifier.STATIC) == Modifier.STATIC)
                System.out.println("这是一个静态方法");
            if ((modifier & Modifier.FINAL) == Modifier.FINAL)
                System.out.println("这是一个final方法");
            if ((modifier & Modifier.ABSTRACT) == Modifier.ABSTRACT)
                System.out.println("这是一个抽象方法");
            if ((modifier & Modifier.SYNCHRONIZED) == Modifier.SYNCHRONIZED)
                System.out.println("这是一个同步方法");
            Class declaringClass = method.getDeclaringClass();
            System.out.println("方法声明在:"+declaringClass.getName()+"中");
            Class[] exceptions = method.getExceptionTypes();
            if (exceptions.length>0){
                System.out.println("该方法抛出的异常有:[");
                for (int i = 0;i <exceptions.length; i++){
                    if (i != 0)
                        System.out.println(",");
                    System.out.println(exceptions[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("-------------------------------");
        }
        //示列6
        Object obj =  clz.newInstance();
        //调用Person类的无参构造创建Person类实例
        System.out.println(obj);
        //调用Person实例的toString()方法,属性均为默认值

        //示列7
        Constructor cl = clz.getDeclaredConstructor();
        System.out.println(obj);
        Constructor c2 = clz.getDeclaredConstructor(String.class);
        c2.setAccessible(true);
        obj=c2.newInstance("New Person");
        System.out.println(obj);
        Constructor c3 = clz.getDeclaredConstructor(String.class,String.class,String.class);
        c3.setAccessible(true);
        obj = c3.newInstance("New Person","beijing","Hello!");
        System.out.println(obj);
        //示列8
        Object person = clz.newInstance();
        Field name = clz.getDeclaredField("name");
        name.setAccessible(true);
        System.out.println("赋值前的name:"+name.get(person));
        name.set(person,"New Person");
        System.out.println("赋值后的name:"+name.get(person));
        //示列9
        Method getAge = clz.getDeclaredMethod("getAge",null);
        getAge.setAccessible(true);
        Object returnAge = getAge.invoke(null,null);
        System.out.println("年龄是:"+returnAge);
        Method silentMethod = clz.getDeclaredMethod("silentMethod",null);
        silentMethod.invoke(person,null);
        Method setName = clz.getDeclaredMethod("setName", String.class);
        setName.invoke(person,"New Person");
        Object returnName = clz.getDeclaredMethod("getName").invoke(person);
        System.out.println("刚才设定的name是:"+returnName);
    }
}
